import EventCenter from "../../TFrame/script/helper/EventCenter";
import DailyRewardManager from "../DailyRewardManager";
import EventNames from "../EventNames";
import { ActionType } from "../net/Enum";
import ServiceLogic from "../net/ServerLogic";
import PlayerDataMgr from "../PlayerDataMgr";

//@ts-ignore
const i18n = require('LanguageData');

const { ccclass, property } = cc._decorator;

@ccclass
export default class DailyRewardDialog extends cc.Component {

    @property(cc.Node)
    layoutRoot: cc.Node = null;

    @property(cc.Label)
    buttonLabel: cc.Label = null;

    rewardDayNum = 9;
    rewardNum = [1, 1, 1, 2, 2, 2, 3, 3, 3];
    rewardNumColor = [cc.Color.GREEN, cc.Color.GREEN, cc.Color.GREEN,
    cc.Color.ORANGE, cc.Color.ORANGE, cc.Color.ORANGE,
    cc.color(255, 0, 255), cc.color(255, 0, 255), cc.color(255, 0, 255)]

    currentCanGetDay = -1;
    onLoad() {
        this.currentCanGetDay = DailyRewardManager.getInstance().currentCanGetDay();
        cc.assetManager.getBundle("GameAb").load("/prefab/dailyItem", cc.Prefab, (err: Error, pf: cc.Prefab) => {
            if (err)
                return console.error(err.message);
            if (this.isValid == false)
                return
            for (let i = 0; i < this.rewardDayNum; ++i) {
                let n = cc.instantiate(pf);
                let title = n.getChildByName("title").getComponent(cc.Label);
                title.string = "Day " + (i + 1).toString();
                let rewardNum = n.getChildByName("num").getComponent(cc.Label);
                rewardNum.string = "x" + this.rewardNum[i];
                rewardNum.node.color = this.rewardNumColor[i]
                let gotMarker = n.getChildByName("right");
                gotMarker.active = DailyRewardManager.getInstance().isDayGot(i)
                n.parent = this.layoutRoot;
                n.name = i.toString();
                if (this.currentCanGetDay == i) {
                    let borderNode = n.getChildByName("border")
                    borderNode.active = true;
                    borderNode.runAction(cc.repeatForever(cc.sequence(cc.fadeTo(0.5, 100), cc.fadeTo(0.5, 255))));
                }
            }
        })
    }

    start() {
        if (this.canGet())
            this.buttonLabel.string = i18n.t("get")
        else
            this.buttonLabel.string = i18n.t("close")
    }

    click(e) {
        if (this.canGet()) {
            console.log(`currentCanGetDay:${this.currentCanGetDay} ${this.rewardNum[this.currentCanGetDay]}`)
            PlayerDataMgr.getInstance().addTips(this.rewardNum[this.currentCanGetDay]);
            DailyRewardManager.getInstance().didGet();
            ServiceLogic.action(ActionType.ActionDailyReward, { 'beiz': this.rewardNum[this.currentCanGetDay] })
            EventCenter.emit("DailyRewardGet")
        }
        this.node.destroy();
    }

    private canGet() {
        return this.currentCanGetDay >= 0 && this.currentCanGetDay < this.rewardDayNum
    }
}
